package com.treasure.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.treasure.common.model.PageResult;
import com.treasure.common.model.SessionUser;
import com.treasure.common.model.SessionUserHolder;
import com.treasure.infrastructure.repository.dao.mapper.CorpTrialApproveMapper;
import com.treasure.web.command.PageConvertor;
import com.treasure.web.command.request.CreateCorpAccountRequest;
import com.treasure.web.command.request.GetTrialApproveRequest;
import com.treasure.web.command.response.CorpTrialApproveResponse;
import com.treasure.web.mapstruct.CorpTrialApproveAssembler;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import com.treasure.web.service.ICorpTrialApproveService;
import com.treasure.infrastructure.model.po.CorpTrialApprove;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sunhongmin
 * @since 2024-01-01
 */
@Service
@RequiredArgsConstructor
public class CorpTrialApproveServiceImpl extends ServiceImpl<CorpTrialApproveMapper, CorpTrialApprove> implements ICorpTrialApproveService {

    private final CorpTrialApproveMapper corpTrialApproveMapper;

    @Override
    public void corpTrialApprove(CreateCorpAccountRequest createCorpAccountRequest) {
        CorpTrialApprove corpTrialApprove = new CorpTrialApprove();
        corpTrialApprove.setCorpName(createCorpAccountRequest.getCorpName());//
        corpTrialApprove.setIndustry(createCorpAccountRequest.getIndustry());
        corpTrialApprove.setPurpose(createCorpAccountRequest.getPurpose());
        corpTrialApprove.setContactor(createCorpAccountRequest.getContactor());//
        corpTrialApprove.setEmail(createCorpAccountRequest.getManageEmail());//
        corpTrialApprove.setCreated(LocalDate.now());
        corpTrialApprove.setApproveStatus(0);
        corpTrialApprove.setApproveUser(null);

        save(corpTrialApprove);
    }

    @Override
    public PageResult<CorpTrialApproveResponse> corpTrialApproveList(GetTrialApproveRequest getTrialApproveRequest) {
        QueryWrapper<CorpTrialApprove> approveQuery = new QueryWrapper<>();
        if (Objects.nonNull(getTrialApproveRequest.getApproveStatus())) {
            approveQuery.lambda().eq(CorpTrialApprove::getApproveStatus, getTrialApproveRequest.getApproveStatus());
        }

        PageHelper.startPage(getTrialApproveRequest.getPage(), getTrialApproveRequest.getPageSize());
        List<CorpTrialApprove> list = corpTrialApproveMapper.selectList(approveQuery);
        PageInfo<CorpTrialApprove> pageInfo = new PageInfo<>(list);

        List<CorpTrialApproveResponse> responseList = CorpTrialApproveAssembler.INSTANCE.convert(list);
        return PageConvertor.convertPage(pageInfo, responseList);
    }

    @Override
    @Transactional
    public void corpTrialApproved(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return;
        }
        SessionUser sessionUser = SessionUserHolder.getAndCheckSessionUser();
        QueryWrapper<CorpTrialApprove> trialApproveQuery = new QueryWrapper<>();
        trialApproveQuery.lambda()
                .in(CorpTrialApprove::getId, idList);
        List<CorpTrialApprove> list = list(trialApproveQuery);
        if (CollectionUtils.isNotEmpty(list)) {
            for (CorpTrialApprove corpTrialApprove : list) {
                CorpTrialApprove update = new CorpTrialApprove();
                update.setId(corpTrialApprove.getId());
                update.setApproveStatus(1);
                update.setApproveUser(sessionUser.getUserId());
                updateById(update);
            }
        }
    }
}
